#!/bin/sh

# directadmin daemon		Start/Stop/Status/Restart

# chkconfig: 2345 80 20
# description: Allows users to modify their websites. \
#              They modify thier: email, subdomains, \
#              databases, dns record, etc...
# processname: directadmin
# config: /usr/local/directadmin/conf/paneld.conf
# pidfile: /var/run/directadmin.pid

# Source function library
. /usr/local/etc/rc.d/functions

PROGBIN="/usr/local/directadmin/directadmin d"
PROGLOCK=/var/spool/lock/directadmin
PROGNAME=directadmin
PROG_PID=/var/run/directadmin.pid

#check the command line for actions

start() {
	echo -n "Starting DirectAdmin: "
	daemon $PROGBIN
	if [ $? = 0 ]; then
		echo "[ OK ]";
	else
		echo "[ FAILED ]";
	fi

	touch $PROGLOCK
}

stop() {
	RET=1;
	echo -n "Stopping DirectAdmin: "
	pid=`cat $PROG_PID`;
	if [ -d /proc/$pid ]; then
		kill -TERM $pid
		RET=$?
	else
		killall -KILL $PROGNAME 1>/dev/null 2>/dev/null
		RET=$?
	fi

	sleep 1;
		
	killall -KILL $PROGNAME 1>/dev/null 2>/dev/null;
		
	if [ $RET = 0 ]; then
		echo "[ OK ]";
	else
		echo "[ FAILED ]";
	fi

	rm -f $PROGLOCK
}

reload() {
        echo -n "Reloading DirectAdmin config file: "
        killall $PROGNAME -HUP
        echo
}

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		status $PROGNAME
		;;
	restart)
		stop
		start
		;;
	reload)
		reload
		;;
	*)
		echo "Usage: $1 {start|stop|status|reload|restart}"
		exit 1
esac

exit 0
